package com.roger.tool.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.telephony.TelephonyManager;

public class IccidUtil {

	public static String getIccid(Context pContext) {
		List<String> iccidList = IccidUtil.getIccidList(pContext);
		String iccid = null;
		if (iccidList.size() > 0) {
			for (int i = 0; i < iccidList.size(); i++) {
				String iccidForList = iccidList.get(i);
//				System.out.println("iccidForList:" + iccidForList);
				if (iccidForList != null && iccidForList.length() >= 20) {
					iccid = iccidForList;
					break;
				}
			}
		}
		if (iccid == null) {
			iccid = "";
		}
		return iccid;
	}

	private static List<String> getIccidList(Context pContext) {
		List<String> iccidList = new ArrayList<String>();
		TelephonyManager telephonyManager = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);

		String simSerialNumber_1 = telephonyManager.getSimSerialNumber();

		Method getSimSerialNumberGeminiMethod = null;
		Method getSimSerialNumberMethod = null;
		try {
			getSimSerialNumberGeminiMethod = TelephonyManager.class.getDeclaredMethod("getSimSerialNumberGemini", new Class[] { Integer.TYPE });
		} catch (Exception e) {
//			e.printStackTrace();
		}
		if (getSimSerialNumberGeminiMethod == null)
			try {
				getSimSerialNumberMethod = TelephonyManager.class.getDeclaredMethod("getSimSerialNumber", new Class[] { Integer.TYPE });
			} catch (Exception e) {
//				e.printStackTrace();
			}
		try {
			if ((simSerialNumber_1 != null) && (simSerialNumber_1.length() > 0)) {
				iccidList.add(simSerialNumber_1);
			}
			String simSerialNumber_2;
			if (getSimSerialNumberGeminiMethod != null) {
				simSerialNumber_2 = (String) getSimSerialNumberGeminiMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(0) });
				if ((simSerialNumber_2 != null) && (!simSerialNumber_2.equals(simSerialNumber_1))) {
					iccidList.add(simSerialNumber_2);
				}
				simSerialNumber_2 = (String) getSimSerialNumberGeminiMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(1) });
				if ((simSerialNumber_2 != null) && (!simSerialNumber_2.equals(simSerialNumber_1))) {
					iccidList.add(simSerialNumber_2);
				}
			} else if (getSimSerialNumberMethod != null) {
				simSerialNumber_2 = (String) getSimSerialNumberMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(0) });
				if ((simSerialNumber_2 != null) && (!simSerialNumber_2.equals(simSerialNumber_1))) {
					iccidList.add(simSerialNumber_2);
				}
				simSerialNumber_2 = (String) getSimSerialNumberMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(1) });
				if ((simSerialNumber_2 != null) && (!simSerialNumber_2.equals(simSerialNumber_1))) {
					iccidList.add(simSerialNumber_2);
				}
			} else {
				simSerialNumber_2 = telephonyManager.getSimSerialNumber();
				if ((simSerialNumber_2 != null) && (!simSerialNumber_2.equals(simSerialNumber_1))) {
					iccidList.add(simSerialNumber_2);
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return iccidList;
	}

}
